home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d26 / inf_src.arc / GETKEYWO.C < prev    next >
C/C++ Source or Header  |  1986-03-14  |  3KB  |  148 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "expert.h"
  5.  
  6.  
  7. /******************************************************************
  8. **
  9. **
  10. **        GET KEYWORD
  11. **
  12. **
  13. ******************************************************************/
  14.  
  15. /*
  16. **
  17. **    getKeyWord(infile,keyWords,lineNum) ;
  18. **
  19. **    returns either KEY_EOF, or number of keyword, or indications
  20. **        that keyword was not first word on line.
  21. **
  22. */
  23.  
  24. int    getKeyWord (infile,keyWords,lineNum)
  25.  
  26. FILE    *infile ;
  27. char    *keyWords[] ;
  28. int    *lineNum ;
  29.  
  30. {
  31. int    firstc,c,i,j ;
  32. #ifdef    DEBUG
  33.     fprintf(stdout,"\nDEBUG-GET_KEYWORD") ;
  34. #endif
  35.  
  36. /*
  37. **    get first non blank character
  38. */
  39.  
  40. i = 0 ;
  41. while( TRUE )
  42.     {
  43.     c = getc(infile);
  44.     putchar(c) ;
  45. #ifdef    DEBUG
  46.     fprintf(stdout,"\nDEBUG-GET_KEYWORD character= %c,%x",c,c) ;
  47. #endif
  48.     if( c == EOF )
  49.         return (KEY_EOF) ;
  50.     if( (i == 0) && (c == COMMENT_CHAR) )    /* ignore comment line */
  51.         {
  52.         while(  c != EOL )
  53.             {
  54. #ifdef DEBUG
  55.     fprintf(stdout,"\nDEBUG-GET_KEYWORD (delete comment) character=%c,%x",c,c);
  56. #endif
  57.             c = getc(infile) ;
  58.             if( c == EOF)
  59.                 return (KEY_EOF) ;
  60.             putchar(c) ;
  61.             } 
  62.         printf("%04d  ",*lineNum) ;
  63.         *lineNum += 1 ;
  64.         i = 0 ;
  65.         }
  66.     else
  67.         {
  68.         i = 1 ;
  69.         if( c != BLANK )
  70.             {
  71.             if(c == EOF)
  72.                 return(KEY_EOF) ;
  73.             break ;
  74.             }
  75.         }
  76.     }
  77.  
  78. /*
  79. **    locate first keyword with matching first character
  80. */
  81.  
  82. for( i = 0 ; i < NUM_KEYWORDS ; i++ )
  83.     {
  84. #ifdef    DEBUG
  85.     fprintf(stdout,"\nDEBUG-GET_KEYWORD i=%d, keyword=%c",i,*(keyWords[i])) ;
  86. #endif
  87.     if ( *(keyWords[i]) == c )
  88.         break ;
  89.     }
  90. if( i == NUM_KEYWORDS )
  91.     return (KEY_WORD_ERROR)  ;
  92.  
  93. /*
  94. **    find the key word if there
  95. **
  96. **    Note that this search algrorithm is very dependant on having
  97. **    the keyWord file being in strict alphabetical order!!!
  98. **
  99. */
  100.  
  101. j=0 ;         
  102. firstc = c ;
  103.  
  104. while( i < NUM_KEYWORDS  )
  105.     {
  106.     if( (c=getc(infile)) == *(keyWords[i]+(++j)) )
  107.         {
  108. #ifdef    DEBUG
  109.     fprintf(stdout,"\nDEBUG-GET_KEYWORD char=%c,keyword=%c",c,*((keyWords[i])+j)) ;
  110. #endif
  111.         putchar(c) ;
  112.         if( c == BLANK )
  113.             return (i) ;
  114.         if( c == EOF )
  115.             return(KEY_EOF) ;
  116.         }
  117. /*
  118. **    increment the keyWord pointer to be tested
  119. **    decrement the character pointer in keyWord
  120. **    and put the last character tested back.
  121. */
  122.  
  123. /*
  124. **    since the keyWord array is alphabetical, testing
  125. **    for a first character change would indicate that
  126. **    the possible keywords have been exhausted.
  127. */
  128.  
  129.     else
  130.         {
  131.         i++ ;
  132.         j-- ;
  133.         ungetc(c,infile) ;
  134.         if( c == EOF )
  135.             return (KEY_EOF) ;
  136.         if( firstc != *(keyWords[i]))
  137.             return (KEY_WORD_ERROR) ;
  138.         }
  139.     }
  140.  
  141. /*
  142. **    no keywords were found because we exhausted the keyWord array
  143. */
  144.  
  145. return (KEY_WORD_ERROR)  ;
  146. }
  147.  
  148.